package HomeElements;

/**
 * Created by zhe.xiao on 2017/6/26.
 */

import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;

import java.util.ArrayList;
import java.util.List;

import static HomeElements.AssertBySelectorElementType.*;
import static HomeElements.Elements.guangGao1Element;
import static HomeElements.Elements.guangGao2Element;
import static HomeElements.Elements.guangGao3Element;
import static HomeElements.Elements.guangGao4Element;
import static HomeElements.Elements.guangGao5Element;
import static HomeElements.Elements.guangGao6Element;
import static HomeElements.Elements.guangGao7Element;
import static HomeElements.Elements.guangGao8Element;
import static util.GetDevice.getDevice;
import static HomeElements.Elements.waitElement;

public class AssertElementsContent {
    private static UiDevice device = getDevice();

    /**
     * wifi的验证信息
     */
    public static String wifiDes() {
        waitElement(wifiInfo());
        String wifiInfo = device.findObject(wifiInfo()).getContentDescription();
        return wifiInfo;
    }

    /**
     * 出境游验证信息
     */
    public static String chuJingDes() {
        waitElement(chuJingInfo());
        String info = device.findObject(chuJingInfo()).getContentDescription();
        return info;
    }

    /**
     * 接机送验证信息
     */
    public static String jieJiSongDes() {
        waitElement(jieJiSongInfo());
        String info = device.findObject(jieJiSongInfo()).getContentDescription();
        return info;
    }

    /**
     * 一日游验证信息
     */
    public static String yiRiYouDes() {
        waitElement(yiRiYouInfo());
        String info = device.findObject(yiRiYouInfo()).getContentDescription().split(" ")[1].trim();
        return info;
    }

    /**
     * 智慧旅游验证信息
     */
    public static String zhiHuiLvYouDes() {
        waitElement(zhiHuiLvYouInfo());
        String info = device.findObject(zhiHuiLvYouInfo()).getContentDescription();
        return info;
    }

    /**
     * 定制游验证信息
     */
    public static String dingZhiYouDes() {
        waitElement(dingZhiYouInfo());
        String info = device.findObject(dingZhiYouInfo()).getContentDescription();
        return info;
    }

    /**
     * 特色住验证信息
     */
    public static String teSeZhuDes() {
//        waitElement(teSeZhuInfo());
        String eleText = "";
        try {
            UiObject ele = device.findObject(teSeZhuInfo());
            eleText = ele.getText();
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
        return eleText;
    }

    /**
     * 领券中心验证信息
     */
    public static String lingQuanZhongXinDes() {
        waitElement(lingQuanZhongXinInfo());
        String info = device.findObject(lingQuanZhongXinInfo()).getContentDescription();
        return info;
    }

    /**
     * 踏青赏花验证信息
     */
    public static String taQingShangHuaDes() {
        waitElement(taQingShangHuaInfo());
        String info = device.findObject(taQingShangHuaInfo()).getContentDescription();
        return info;
    }

    /**
     * 特价游验证信息
     */
    public static String teJiaYouDes() {
        waitElement(teJiaYouInfo());
        String info = device.findObject(teJiaYouInfo()).getContentDescription().trim();
        return info;
    }

    /**
     * 您可能想去tag1内容
     */
    public static String ninKeTag1Des() {
        waitElement(ninTag1Info());
        String info = device.findObject(ninTag1Info()).getChildren().get(0).getText().trim();
        return info;
    }

    /**
     * 您可能想去跳转后tag1内容
     */
    public static String ninKeTag1TiaoDes() {
        waitElement(ninTag1TiaoInfo());
        String info = device.findObject(ninTag1TiaoInfo()).getChildren().get(1).getChildren().get(1).getContentDescription();
        return info;
    }

    /**
     * 您可能想去tag2内容
     */
    public static String ninKeTag2Des() {
        waitElement(ninTag1Info());
        String info = device.findObject(ninTag1Info()).getChildren().get(1).getText().trim();
        return info;
    }

    /**
     * 您可能想去跳转后tag2内容
     */
    public static String ninKeTag2TiaoDes() {
        waitElement(ninTag1TiaoInfo());
        String info = device.findObject(ninTag1TiaoInfo()).getChildren().get(1).getChildren().get(1).getContentDescription();
        return info;
    }

    /**
     * 您可能想去tag3内容
     */
    public static String ninKeTag3Des() {
        waitElement(ninTag1Info());
        String info = device.findObject(ninTag1Info()).getChildren().get(2).getText().trim();
        return info;
    }

    /**
     * 您可能想去跳转后tag3内容
     */
    public static String ninKeTag3TiaoDes() {
        waitElement(ninTag1TiaoInfo());
        String info = device.findObject(ninTag1TiaoInfo()).getChildren().get(1).getChildren().get(1).getContentDescription();
        return info;
    }

    /**
     * 测试您可能想去tag的数量
     */
    public static int ninKeNengTagNumDes() {
        waitElement(ninTag1Info());
        int info = device.findObject(ninTag1Info()).getChildCount();
        return info;
    }

    /**
     * 最近关注全部按钮验证
     */
    public static String zuiJinGuanZhuDes() {
        waitElement(zuiJinGuanZhuInfo());
        String info = device.findObject(zuiJinGuanZhuInfo()).getContentDescription().trim();
        return info;
    }

    /**
     * 最近关注酒店验证信息
     */
    public static String zuiJinGuanZhuHotelDes() {
        waitElement(zuiJinGuanZhuHotelInfo());
        String info = device.findObject(zuiJinGuanZhuHotelInfo()).getText();
        return info;
    }

    /**
     * 最近关注门票验证信息
     */
    public static String zuiJinGuanZhuTicketDes() {
        waitElement(zuiJinGuanZhuTicketInfo());
        sleep(5000);
        UiObject2 ele = device.findObject(zuiJinGuanZhuTicketInfo()).getChildren().get(1);
        UiObject2 ele1 = ele.getChildren().get(1);
        String info = ele1.getContentDescription();
        return info;
    }

    /**
     * 最近关注度假
     */
    public static String zuiJinGuanZhuVdes() {
        waitElement(zuiJinGuanZhuVInfo());
        UiObject2 ele = device.findObject(zuiJinGuanZhuVInfo());
        UiObject2 ele1 = ele.getChildren().get(12);
        String info = ele1.getContentDescription().replace("“", "").replace("”", "");
        return info;
    }

    /**
     * 最近关注 第一个标题
     */
    public static String zuiJinGuanZhu1Tile() {
        UiObject2 ele = device.findObject(zuiJinGuanZhuYanInfo()).getChildren().get(0);
        String info = ele.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_recent_product_title")).getText().trim().split("]")[1];
        return info;
    }

    /**
     * 最近关注 第二个标题
     */
    public static String zuiJinGuanZhu2Tile() {
        UiObject2 ele = device.findObject(zuiJinGuanZhuYanInfo()).getChildren().get(1);
        String info = ele.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_recent_product_title")).getText().trim().split("]")[1];
        return info;
    }

    /**
     * 最近关注 第一个金额
     */
    public static String zuiJinGuanZhu1Mon() {
        UiObject2 ele = device.findObject(zuiJinGuanZhuYanInfo()).getChildren().get(0);
        String info = ele.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_recent_price")).getText();
        return info;
    }

    /**
     * 最近关注 第二个金额
     */
    public static String zuiJinGuanZhu2Mon() {
        UiObject2 ele = device.findObject(zuiJinGuanZhuYanInfo()).getChildren().get(1);
        String info = ele.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_recent_price")).getText();
        return info;
    }

    /**
     * 最近关注 第一个分数
     */
    public static String zuiJinGuanZhu1Sou() {
        UiObject2 ele = device.findObject(zuiJinGuanZhuYanInfo()).getChildren().get(0);
        String info = ele.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_recent_product_salecount")).getText();
        return info;
    }

    /**
     * 最近关注 第二个分数
     */
    public static String zuiJinGuanZhu2Sou() {
        String info = "";
        UiObject2 ele = device.findObject(zuiJinGuanZhuYanInfo()).getChildren().get(1);
        try {
            info = ele.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_recent_product_salecount")).getText();
        } catch (Exception e) {

        }

        return info;
    }

    private static void sleep(int i) {
        try {
            Thread.sleep(i);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 热门专题更多验证
     */
    public static boolean reMenMoreDes() {
        waitElement(renMenMoreInfo());
        UiObject2 ele = device.findObject(renMenMoreInfo());
        boolean t = ele.isClickable();
        return t;
    }

    /**
     * 热门专题tag跳转
     */
    public static boolean reMenZhuanTagDes() {
        waitElement(renMenZhuanTagInfo());
        UiObject2 ele = device.findObject(renMenZhuanTagInfo());
        boolean t = ele != null;
        return t;
    }

    /**
     * 热门目的地更多验证
     */
    public static boolean reMenMuMoreDes() {
        waitElement(renMenMuMoreInfo());
        UiObject2 ele = device.findObject(renMenMuMoreInfo());
        boolean t = ele.isClickable();
        return t;
    }

    /**
     * 热门目的地验证
     */
    public static String reMenMuDes() {
        UiObject ele = device.findObject(reMenMuCityInfo());
        String info = null;
        try {
            info = ele.getChild(new UiSelector().className("android.view.View").instance(1)).getChild(new UiSelector().className("android.view.View").instance(0)).getContentDescription().trim();
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
        return info;
    }

    /**
     * 热门目的地第一个城市按钮
     */
    public static String reMenMuCity1Des() {
        waitElement(reMenMuCity());
        UiObject2 ele = device.findObject(reMenMuCity()).getParent().getParent().getParent().getChildren().get(1);
        UiObject2 ele1 = ele.getChildren().get(0).findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_search_item_title"));
        String info = ele1.getText().trim();
        return info;
    }

    /**
     * 热门目的地第二个城市按钮
     */
    public static String reMenMuCity2Des() {
        waitElement(reMenMuCity());
        UiObject2 ele = device.findObject(reMenMuCity()).getParent().getParent().getParent().getChildren().get(1);
        UiObject2 ele1 = ele.getChildren().get(1).findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_search_item_title"));
        String info = ele1.getText().trim();
        return info;
    }

    /**
     * 热门目的地第三个城市按钮
     */
    public static String reMenMuCity3Des() {
        waitElement(reMenMuCity());
        UiObject2 ele = device.findObject(reMenMuCity()).getParent().getParent().getParent().getChildren().get(1);
        UiObject2 ele1 = ele.getChildren().get(2).findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_search_item_title"));
        String info = ele1.getText().trim();
        return info;
    }

    /**
     * 6+1广告第一个
     */
    public static List<String> guangGao1Des() {
        List<String> l = new ArrayList<>();
        UiObject2 ele = guangGao1Element();
        String title = ele.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_bargain_title")).getText().trim();
        String sub_title = ele.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_bargain_description")).getText().trim();
        l.add(title);
        l.add(sub_title);
        return l;
    }

    public static String guangGao1Des1() {
        waitElement(guangGao1Info());
        UiObject2 ele = device.findObject(guangGao1Info());
        String info = ele.getContentDescription();
        return info;
    }

    /**
     * 6+1广告第二个
     */
    public static List<String> guangGao2Des() {
        List<String> l = new ArrayList<>();
        UiObject2 ele = guangGao2Element();
        String title = ele.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_marketing_name")).getText().trim();
        String sub_title = ele.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_marketing_desc")).getText().trim();
        l.add(title);
        l.add(sub_title);
        return l;
    }

    public static boolean guangGaoTongDes() {
        sleep(5000);
        UiObject2 ele = device.findObject(guangGaoTInfo());
        if (ele == null) {
            return true;
        }
        return false;
    }

    /**
     * 6+1广告第三个
     */
    public static List<String> guangGao3Des() {
        List<String> l = new ArrayList<>();
        UiObject2 ele = guangGao3Element();
        String title = ele.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_marketing_name")).getText().trim();
        String sub_title = ele.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_marketing_desc")).getText().trim();
        l.add(title);
        l.add(sub_title);
        return l;
    }

    /**
     * 6+1广告第四个
     */
    public static List<String> guangGao4Des() {
        List<String> l = new ArrayList<>();
        UiObject2 ele = guangGao4Element();
        String title = ele.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_marketing_name")).getText().trim();
        String sub_title = ele.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_marketing_desc")).getText().trim();
        l.add(title);
        l.add(sub_title);
        return l;
    }

    /**
     * 6+1广告第五个
     */
    public static List<String> guangGao5Des() {
        List<String> l = new ArrayList<>();
        UiObject2 ele = guangGao5Element();
        String title = ele.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_marketing_name")).getText().trim();
        String sub_title = ele.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_marketing_desc")).getText().trim();
        l.add(title);
        l.add(sub_title);
        return l;
    }

    /**
     * 6+1广告第六个
     */
    public static List<String> guangGao6Des() {
        List<String> l = new ArrayList<>();
        UiObject2 ele = guangGao6Element();
        String title = ele.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_marketing_name")).getText().trim();
        String sub_title = ele.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_marketing_desc")).getText().trim();
        l.add(title);
        l.add(sub_title);
        return l;
    }

    /**
     * 6+1广告第七个
     */
    public static List<String> guangGao7Des() {
        List<String> l = new ArrayList<>();
        UiObject2 ele = guangGao7Element();
        String title = ele.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_marketing_name")).getText().trim();
        String sub_title = ele.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_marketing_desc")).getText().trim();
        l.add(title);
        l.add(sub_title);
        return l;
    }

    /**
     * 6+1广告第八个
     */
    public static String guangGao8Des() {
        UiObject2 ele = guangGao8Element();
        String title = ele.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_viewflipper_content")).getText().trim();
        return title;
    }

    /**
     * 如果标题包含页面不存在返回True
     */
    public static boolean pushDes() {
        boolean b = false;
        int t = -1;
        sleep(5000);
        UiObject2 u2 = device.findObject(pushInfo());
        if (u2 != null) {
            t = u2.getText().indexOf("页面不存在");
        }

        if (t != -1) {
            b = true;
        }
        return b;
    }

    /**
     * 出现加载失败
     */
    public static boolean bannerInternetDes() {
        boolean b = false;
        UiObject2 u2 = device.findObject(By.text("重试"));
        if (u2 == null) {
            u2 = device.findObject(By.desc("重试"));
        }
        if (u2 == null) {
            u2 = device.findObject(By.descContains("重试"));
        }
        if (u2 == null) {
            UiObject2 ele = device.findObject(By.res("com.mqunar.hy:id/browser_title_text"));
            if (ele != null) {
                String text = ele.getText();
                String text1 = ele.getContentDescription();
                if (text != null) {
                    if (text.indexOf("404") != -1) {
                        b = true;
                    }
                    if (text.indexOf("502") != -1) {
                        b = true;
                    }
                }
                if (text1 != null) {
                    if (text1.indexOf("404") != -1) {
                        b = true;
                    }
                    if (text1.indexOf("502") != -1) {
                        b = true;
                    }
                }
            }
        }
        if (u2 != null) {
            b = u2.isEnabled();
        }
        return b;
    }
}
